﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BAL;
using DAL;
using DevExpress.XtraGrid.Menu;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyPhongTroDevLinQ
{
    public partial class Frm_Main : Form
    {

        BAL_PhieuThu _balPhieuThu = new BAL_PhieuThu();
        BAL_DichVu _balDichVu = new BAL_DichVu();
        BAL_HoaDon _balHoaDon = new BAL_HoaDon();
        QuanLyPhongTroDBDataContext dbs1 = new QuanLyPhongTroDBDataContext();
        // Ty Gia Dich Vu
        BAL_TyGiaDichVu _balTyGiaDichVu = new BAL_TyGiaDichVu();
        public static TyGiaDichVu _tygiaDichVu;
        public static PhieuThu staticPhieuThu;
        public static HoaDon staticHoaDon;
        public static SuDungDichVu staticSDDien;
        public static SuDungDichVu staticSDNuoc;



        public Frm_Main()
        {
            InitializeComponent();
            gridViewMain.OptionsBehavior.ReadOnly = true;
            navBarGroupHoaDon.Expanded = false;
            navBarGroupPhieuThu.Expanded = false;
        }

        #region Phieu Thu
        private void barButtonDSPhieuThu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // Load data to gridview

            gridViewMain.Columns.Clear();
            gridControlMain.DataSource = _balPhieuThu.LayPhieuThu();
            gridViewMain.Columns[4].Visible = false;// Enable barbutton
            barButtonThemPhieuThu.Enabled = true;
            barButtonCapNhatPhieuThu.Enabled = true;
            barButtonXoaPhieuThu.Enabled = true;
            //
            barButtonThemTyGia.Enabled = false;
            barButtonSuaTyGia.Enabled = false;
            barButtonXoaTyGia.Enabled = false;

            barButtonThemHoaDon.Enabled = false;
            barButtonCapNhatHoaDon.Enabled = false;
            barButtonXoaHoaDon.Enabled = false;
            //
            navBarGroupHoaDon.Expanded = false;
            navBarGroupPhieuThu.Expanded = true;
        }

        private void barButtonThemPhieuThu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {


            Frm_ThemPhieuThu._capNhat = false;
            Frm_ThemPhieuThu frmThemPhieu;
            if (this.barButtonThemHoaDon.Enabled == true)
            {
                try
                {
                    int[] index = gridViewMain.GetSelectedRows();
                    string maHoaDon = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[0]).ToString();
                    frmThemPhieu = new Frm_ThemPhieuThu(maHoaDon);
                }
                catch (Exception)
                {

                    frmThemPhieu = new Frm_ThemPhieuThu();
                }

            }
            else
            {
                frmThemPhieu = new Frm_ThemPhieuThu();
            }
            DialogResult kq = frmThemPhieu.ShowDialog();

            if (kq == DialogResult.OK)
            {
                gridControlMain.DataSource = _balPhieuThu.LayPhieuThu();
            }

        }

        private void barButtonXoaPhieuThu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                staticPhieuThu = (PhieuThu)gridViewMain.Columns[0].View.GetFocusedRow();
                DialogResult kq = MessageBox.Show("Có xóa phiếu thu : " +
                    staticPhieuThu.MaPhieuThu.ToString()
                    + " không ?",
                    "Xóa Phiếu Thu",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                if (kq == DialogResult.Yes)
                {
                    _balPhieuThu.XoaPhieuThu(staticPhieuThu.MaPhieuThu.ToString());
                    MessageBox.Show("Xóa Thành Công.",
                        "Kết quả",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    gridControlMain.DataSource = _balPhieuThu.LayPhieuThu();
                }
            }
            catch
            {
                MessageBox.Show("Xóa Không Thành Công.",
                        "Lỗi",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
            }
        }

        private void barButtonCapNhatPhieuThu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_ThemPhieuThu._capNhat = true;
            staticPhieuThu = (PhieuThu)gridViewMain.Columns[0].View.GetFocusedRow();
            Frm_ThemPhieuThu frmThemPhieu = new Frm_ThemPhieuThu();
            DialogResult kq = frmThemPhieu.ShowDialog();

            if (kq == DialogResult.OK)
            {
                BAL_PhieuThu ba=new BAL_PhieuThu();
                gridControlMain.DataSource = ba.LayPhieuThu();
                Frm_ThemPhieuThu._capNhat = false;
            }
        }
        #endregion
        #region TyGiaDichVu
        private void barButtonDanhSachDichVu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridViewMain.Columns.Clear();
            gridControlMain.DataSource = _balTyGiaDichVu.LayTyGiaDichVu();
            //

            barButtonThemTyGia.Enabled = true;
            barButtonSuaTyGia.Enabled = true;
            barButtonXoaTyGia.Enabled = true;

            barButtonThemHoaDon.Enabled = false;
            barButtonCapNhatHoaDon.Enabled = false;
            barButtonXoaHoaDon.Enabled = false;

            barButtonThemPhieuThu.Enabled = false;
            barButtonCapNhatPhieuThu.Enabled = false;
            barButtonXoaPhieuThu.Enabled = false;

        }

        private void barButtonThemTyGia_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_TyGiaDichVu._capnhatTyGia = false;
            Frm_TyGiaDichVu frm_tygiaDV = new Frm_TyGiaDichVu();
            DialogResult dgResult = frm_tygiaDV.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridControlMain.DataSource = _balTyGiaDichVu.LayTyGiaDichVu();
            }
        }

        private void barButtonSuaTyGia_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_TyGiaDichVu._capnhatTyGia = true;
            int[] index = gridViewMain.GetSelectedRows();
            string maDV = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[0]).ToString();
            DateTime ngayCN = (DateTime)gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[3]);
            string tyGia = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[2]).ToString();
            _tygiaDichVu = new TyGiaDichVu() { MaDichVu = maDV, TyGia = int.Parse(tyGia), NgayCapNhat = ngayCN };
            Frm_TyGiaDichVu frm_tygiaDV = new Frm_TyGiaDichVu();
            DialogResult dgResult = frm_tygiaDV.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridControlMain.DataSource = _balTyGiaDichVu.LayTyGiaDichVu();
            }

        }

        private void barButtonXoaTyGia_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] index = gridViewMain.GetSelectedRows();
            string maDV = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[0]).ToString();
            DateTime ngayCN = (DateTime)gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[3]);


            try
            {
                DialogResult kq = MessageBox.Show("Có xóa tỷ giá : " +
                    maDV.Trim() + " cập nhật ngày " + ngayCN.Date.ToString("dd-MM-yyyy")
                    + " không ?",
                    "Xóa Tỷ Giá",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                if (kq == DialogResult.Yes)
                {
                    _balTyGiaDichVu.XoaTyGiaDichVu(maDV, ngayCN);
                    MessageBox.Show("Xóa Thành Công.",
                        "Kết quả",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    gridControlMain.DataSource = _balTyGiaDichVu.LayTyGiaDichVu();
                }
            }
            catch
            {
                MessageBox.Show("Xóa Không Thành Công.",
                        "Lỗi", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
            }
        }

        #endregion

        #region HoaDon
        private void barButtonDSHoaDon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            gridViewMain.Columns.Clear();
            gridControlMain.DataSource = _balHoaDon.LayDSHoaDon();

            //
            barButtonThemHoaDon.Enabled = true;
            barButtonCapNhatHoaDon.Enabled = true;
            barButtonXoaHoaDon.Enabled = true;
            //
            barButtonThemTyGia.Enabled = false;
            barButtonSuaTyGia.Enabled = false; barButtonXoaTyGia.Enabled = false;

            //
            barButtonThemPhieuThu.Enabled = true;
            barButtonCapNhatPhieuThu.Enabled = false;
            barButtonXoaPhieuThu.Enabled = false;
            //
            navBarGroupHoaDon.Expanded = true;
            navBarGroupPhieuThu.Expanded = false;
        }



        private void barButtonThemHoaDon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            Frm_ThemHoaDon._isUpdate = false;

            Frm_ThemHoaDon frm_ThemHoaDon = new Frm_ThemHoaDon();
            DialogResult dgResult = frm_ThemHoaDon.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = _balHoaDon.LayDSHoaDon();
            }
        }
        #endregion

        private void barButtonCapNhatHoaDon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Frm_ThemHoaDon._isUpdate = true;
            int[] index = gridViewMain.GetSelectedRows();
            string maHoaDon = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[0]).ToString();
            DateTime ngayLap = (DateTime)gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[2]);
            string maPhong = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[1]).ToString();
            int sdDien = int.Parse(gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[3]).ToString());
            int sdNuoc = int.Parse(gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[6]).ToString());
            staticHoaDon = new HoaDon() { MaHoaDon = maHoaDon, MaPhong = maPhong, NgayLap = ngayLap };
            staticSDDien = new SuDungDichVu() { MaHoaDon = maHoaDon, MaDichVu = "01", SoLuongSD = sdDien };
            staticSDNuoc = new SuDungDichVu() { MaHoaDon = maHoaDon, MaDichVu = "02", SoLuongSD = sdNuoc };

           
            // show dialog
            Frm_ThemHoaDon frm_ThemHoaDon = new Frm_ThemHoaDon();
            DialogResult dgResult = frm_ThemHoaDon.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
              //  gridViewMain.Columns.Clear();
                gridControlMain.DataSource = _balHoaDon.LayDSHoaDon();
            }


        }

        private void barButtonXoaHoaDon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int[] index = gridViewMain.GetSelectedRows();
            string maHoaDon = gridViewMain.GetRowCellValue(index[0], gridViewMain.Columns[0]).ToString();

            try
            {
                DialogResult kq = MessageBox.Show("Có xóa hóa đơn : " +
                    maHoaDon.Trim() + " không ?",
                    "Xóa hóa đơn",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);
                if (kq == DialogResult.Yes)
                {
                    _balHoaDon.XoaHoaDon(maHoaDon.Trim());
                    MessageBox.Show("Xóa Thành Công.",
                        "Kết quả",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                    gridControlMain.DataSource = _balHoaDon.LayDSHoaDon();
                }
            }
            catch
            {
                MessageBox.Show("Xóa Không Thành Công.",
                             "Lỗi", MessageBoxButtons.OK,
                             MessageBoxIcon.Error);
            }
        }

        #region Tim kiem hoa don
        private void nvbTimKiemHDtheoThang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimKiemHoaDon frm_TimKiemHoaDon = new Frm_TimKiemHoaDon(0);
            DialogResult dgResult = frm_TimKiemHoaDon.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimKiemHoaDon.timkiemHoaDon;
            }
        }

        private void nvbTimKiemHDtheoMaHD_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimKiemHoaDon frm_TimKiemHoaDon = new Frm_TimKiemHoaDon(1);
            DialogResult dgResult = frm_TimKiemHoaDon.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimKiemHoaDon.timkiemHoaDon;
            }
        }

        private void nvbTimKiemHDtheoPhong_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimKiemHoaDon frm_TimKiemHoaDon = new Frm_TimKiemHoaDon(2);
            DialogResult dgResult = frm_TimKiemHoaDon.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimKiemHoaDon.timkiemHoaDon;
            }
        }
        #endregion
        #region Tim Phieu Thu
        private void navTimKiemPhieuThuThang_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimPhieuThu frmTimPhieuThu = new Frm_TimPhieuThu(0);
            DialogResult dgResult = frmTimPhieuThu.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimPhieuThu.timkiemPhieuThu;
            }
        }

        private void navTimKiemPhieuThuMaHD_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimPhieuThu frmTimPhieuThu = new Frm_TimPhieuThu(1);
            DialogResult dgResult = frmTimPhieuThu.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimPhieuThu.timkiemPhieuThu;
            }

        }

        private void navTimKiemPhieuThuMaPT_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Frm_TimPhieuThu frmTimPhieuThu = new Frm_TimPhieuThu(2);
            DialogResult dgResult = frmTimPhieuThu.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                gridViewMain.Columns.Clear();
                gridControlMain.DataSource = Frm_TimPhieuThu.timkiemPhieuThu;
            }

        }
        #endregion

        private void navBarItemReportHoaDon_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {Frm_ReportHoaDon frm_hoadon=new Frm_ReportHoaDon();
            frm_hoadon.Show();
        }
    }
}
